from typing import List


class Solution:
    def countNegatives(self, grid: List[List[int]]) -> int:
        if not grid:
            return 0
        m, n = len(grid), len(grid[0])
        ans = 0
        for i in range(m):
            left, right = 0, n - 1
            rowAns = -1
            while left <= right:
                mid = (left + right) // 2
                if grid[i][mid] < 0:
                    rowAns = mid
                    right = mid - 1
                else:
                    left = mid + 1
            if rowAns > -1:
                ans += n - left
        return ans